package com.hr.剑指offer.work2;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：zhz
 * @date ：Created in 2020/12/04
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description: 两数之和
 **/
public class TwoSum_1 {
    public static int[] twoSum(int[] nums, int target) {
        int[] res = new int[2];
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (target - nums[i] == nums[j]) {
                    res[0] = i;
                    res[1] = j;
                }
            }
        }
        return res;
    }

    public static int[] twoSum1(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        int[] res = new int[2];
        int tmp;
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);//2,0   7,1  11,2   15,3
        }
        for (int j = 0; j < nums.length; j++) {
            tmp = target - nums[j];//9-2 9-7
            if (map.containsKey(tmp) && map.get(tmp) != j) {
                res[0] = map.get(tmp);
                res[1] = j;
            }
        }
        return res;
    }
}